package com.exemplo.flickrsocial.view;

import javax.xml.parsers.ParserConfigurationException;

import com.gmail.yuyang226.flickr.Flickr;
import com.gmail.yuyang226.flickr.REST;
import com.gmail.yuyang226.flickr.RequestContext;
import com.gmail.yuyang226.flickr.interestingness.InterestingnessInterface;
import com.gmail.yuyang226.flickr.oauth.OAuth;
import com.gmail.yuyang226.flickr.oauth.OAuthToken;
import com.gmail.yuyang226.flickr.photos.PhotosInterface;

/**
 * 
 * @author Douglas
 * 
 */
public final class FlickrHelper {

	private static FlickrHelper instance = null;
	private static final String CONSUMER_KEY = "728dbe8a866dfd273a534a6d520c8d27";
	public static final String CONSUMER_SECRENT_KEY = "a750f57ab0bca25d";

	private FlickrHelper() {

	}

	/**
	 * Get instance of flickHelper
	 * 
	 * @return
	 */
	public static FlickrHelper getInstance() {
		if (instance == null) {
			instance = new FlickrHelper();
		}

		return instance;
	}

	/**
	 * get Flickr
	 * 
	 * @return
	 */
	public Flickr getFlickr() {

		try {
			Flickr flickr = new Flickr(CONSUMER_KEY, CONSUMER_SECRENT_KEY,
					new REST());
			return flickr;

		} catch (ParserConfigurationException e) {
			return null;
		}
	}

	/**
	 * get flick authenticated
	 * 
	 * @param token
	 * @param secret
	 * @return
	 */
	public Flickr getFlickrAuthed(String token, String secret) {

		Flickr flickr = getFlickr();
		RequestContext requestContext = RequestContext.getRequestContext();
		OAuth auth = new OAuth();
		auth.setToken(new OAuthToken(token, secret));
		requestContext.setOAuth(auth);
		return flickr;
	}

	public InterestingnessInterface getInterestingInterface() {

		Flickr flickr = getFlickr();

		if (flickr != null) {
			return flickr.getInterestingnessInterface();

		} else {
			return null;
		}
	}

	/**
	 * get photos.
	 * 
	 * @return
	 */
	public PhotosInterface getPhotosInterface() {
		Flickr flickr = getFlickr();

		if (flickr != null) {
			return flickr.getPhotosInterface();

		} else {
			return null;
		}
	}

}
